table<-function(Q1,Q2,Q3,Q4,Q5,alpha=0.05){
  K<-ncol(Q5)
  Estim<-matrix(rep("",K*5),nrow=K,ncol=5)
  colnames(Estim)<-c("Q1","Q2","Q3","Q4","Total")
  rownames(Estim)<-colnames(Q5)
  
  mQ1<-format(round(apply(4*Q1,2,mean),3),scientific=F)
  mQ1["exper"]<-format(round(apply(4*Q1,2,mean)["exper"],4),scientific=F)
  mQ1["exper2"]<-format(round(apply(4*Q1,2,mean)["exper2"],5),scientific=F)
  
  mQ2<-format(round(apply(4*Q2,2,mean),3),scientific=F)
  mQ2["exper"]<-format(round(apply(4*Q2,2,mean)["exper"],4),scientific=F)
  mQ2["exper2"]<-format(round(apply(4*Q2,2,mean)["exper2"],5),scientific=F)
  
  mQ3<-format(round(apply(4*Q3,2,mean),3),scientific=F)
  mQ3["exper"]<-format(round(apply(4*Q3,2,mean)["exper"],4),scientific=F)
  mQ3["exper2"]<-format(round(apply(4*Q3,2,mean)["exper2"],5),scientific=F)
  
  mQ4<-format(round(apply(4*Q4,2,mean),3),scientific=F)
  mQ4["exper"]<-format(round(apply(4*Q4,2,mean)["exper"],4),scientific=F)
  mQ4["exper2"]<-format(round(apply(4*Q4,2,mean)["exper2"],5),scientific=F)
  
  mTot<-format(round(apply(Q5,2,mean),3),scientific=F)
  mTot["exper"]<-format(round(apply(Q5,2,mean)["exper"],4),scientific=F)
  mTot["exper2"]<-format(round(apply(Q5,2,mean)["exper2"],5),scientific=F)
  
  Estim[,"Q1"]<-mQ1
  Estim[,"Q2"]<-mQ2
  Estim[,"Q3"]<-mQ3
  Estim[,"Q4"]<-mQ4
  Estim[,"Total"]<-mTot
  
  CI_Q1<-apply(4*Q1,2,quantile,probs=c(alpha/2,1-alpha/2))
  CI_Q2<-apply(4*Q2,2,quantile,probs=c(alpha/2,1-alpha/2))
  CI_Q3<-apply(4*Q3,2,quantile,probs=c(alpha/2,1-alpha/2))
  CI_Q4<-apply(4*Q4,2,quantile,probs=c(alpha/2,1-alpha/2))
  CI_Tot<-apply(Q5,2,quantile,probs=c(alpha/2,1-alpha/2))
  
  Stars<-matrix(rep("",K*5),nrow=K,ncol=5)
  colnames(Stars)<-c("Q1","Q2","Q3","Q4","Total")
  rownames(Stars)<-colnames(Q5)
  
  #Adding starts
  # Stars[CI_Q1["2.5%",]*CI_Q1["97.5%",]>0,"Q1"]="*"
  # Stars[CI_Q2["2.5%",]*CI_Q2["97.5%",]>0,"Q2"]="*"
  # Stars[CI_Q3["2.5%",]*CI_Q3["97.5%",]>0,"Q3"]="*"
  # Stars[CI_Q4["2.5%",]*CI_Q4["97.5%",]>0,"Q4"]="*"
  # Stars[CI_Q5["2.5%",]*CI_Q5["97.5%",]>0,"Total"]="*"
  # 
  # Estim<-matrix(paste(Estim,Stars,sep=""),nrow=K,ncol=5)
  # colnames(Estim)<-c("Q1","Q2","Q3","Q4","Total")
  # rownames(Estim)<-colnames(Q5)
  #---
  CI_L<-matrix(rep("",K*5),nrow=K,ncol=5)
  colnames(CI_L)<-c("Q1","Q2","Q3","Q4","Total")
  rownames(CI_L)<-colnames(Q5)
  
  CI_L[,"Q1"]<-format(round(CI_Q1["2.5%",],3),scientific=F)
  CI_L["exper","Q1"]<-format(round(CI_Q1["2.5%","exper"],4),scientific=F)
  CI_L["exper2","Q1"]<-format(round(CI_Q1["2.5%","exper2"],5),scientific=F)
  
  CI_L[,"Q2"]<-format(round(CI_Q2["2.5%",],3),scientific=F)
  CI_L["exper","Q2"]<-format(round(CI_Q2["2.5%","exper"],4),scientific=F)
  CI_L["exper2","Q2"]<-format(round(CI_Q2["2.5%","exper2"],5),scientific=F)
  
  CI_L[,"Q3"]<-format(round(CI_Q3["2.5%",],3),scientific=F)
  CI_L["exper","Q3"]<-format(round(CI_Q3["2.5%","exper"],4),scientific=F)
  CI_L["exper2","Q3"]<-format(round(CI_Q3["2.5%","exper2"],5),scientific=F)
  
  CI_L[,"Q4"]<-format(round(CI_Q4["2.5%",],3),scientific=F)
  CI_L["exper","Q4"]<-format(round(CI_Q4["2.5%","exper"],4),scientific=F)
  CI_L["exper2","Q4"]<-format(round(CI_Q4["2.5%","exper2"],5),scientific=F)
  
  CI_L[,"Total"]<-format(round(CI_Q5["2.5%",],3),scientific=F)
  CI_L["exper","Total"]<-format(round(CI_Q5["2.5%","exper"],4),scientific=F)
  CI_L["exper2","Total"]<-format(round(CI_Q5["2.5%","exper2"],5),scientific=F)
  
  CI_U<-matrix(rep("",K*5),nrow=K,ncol=5)
  colnames(CI_U)<-c("Q1","Q2","Q3","Q4","Total")
  rownames(CI_U)<-colnames(Q5)
  
  CI_U[,"Q1"]<-format(round(CI_Q1["97.5%",],3),scientific=F)
  CI_U["exper","Q1"]<-format(round(CI_Q1["97.5%","exper"],4),scientific=F)
  CI_U["exper2","Q1"]<-format(round(CI_Q1["97.5%","exper2"],5),scientific=F)
  
  CI_U[,"Q2"]<-format(round(CI_Q2["97.5%",],3),scientific=F)
  CI_U["exper","Q2"]<-format(round(CI_Q2["97.5%","exper"],4),scientific=F)
  CI_U["exper2","Q2"]<-format(round(CI_Q2["97.5%","exper2"],5),scientific=F)
  
  CI_U[,"Q3"]<-format(round(CI_Q3["97.5%",],3),scientific=F)
  CI_U["exper","Q3"]<-format(round(CI_Q3["97.5%","exper"],4),scientific=F)
  CI_U["exper2","Q3"]<-format(round(CI_Q3["97.5%","exper2"],5),scientific=F)
  
  CI_U[,"Q4"]<-format(round(CI_Q4["97.5%",],3),scientific=F)
  CI_U["exper","Q4"]<-format(round(CI_Q4["97.5%","exper"],4),scientific=F)
  CI_U["exper2","Q4"]<-format(round(CI_Q4["97.5%","exper2"],5),scientific=F)
  
  CI_U[,"Total"]<-format(round(CI_Q5["97.5%",],3),scientific=F)
  CI_U["exper","Total"]<-format(round(CI_Q5["97.5%","exper"],4),scientific=F)
  CI_U["exper2","Total"]<-format(round(CI_Q5["97.5%","exper2"],5),scientific=F)
  
  CI<-matrix(paste(CI_L,";",CI_U,sep=""),nrow=K,ncol=5)
  colnames(CI)<-c("Q1","Q2","Q3","Q4","Total")
  rownames(CI)<-colnames(Q5)
  
  Table<-matrix(rep("",2*K*5),nrow=2*K,ncol=5)
  colnames(Table)<-colnames(Estim)
  Names<-matrix(rep("",2*K),nrow=2*K,ncol=1)
  Names[seq(1,2*K,by=2)]<-rownames(Estim)
  Names[seq(2,2*K,by=2)]<-paste("CI_",rownames(Estim),sep="")
  rownames(Table)<-Names
  
  Table[seq(1,2*K,by=2),]<-Estim
  Table[seq(2,2*K,by=2),]<-CI
  
  FinalTable<-matrix(rep("",2*K*5),nrow=2*K,ncol=5)
  colnames(FinalTable)<-colnames(Table)
  FinalNames<-matrix(rep("",2*K),nrow=2*K,ncol=1)
  FinalNames[seq(1,2*K-2,by=1),]<-Names[seq(3,2*K,by=1),]
  FinalNames[seq(2*K-1,2*K,by=1),]<-Names[c(1,2),]
  rownames(FinalTable)<-FinalNames
  FinalTable[seq(1,2*K-2,by=1),]<-Table[seq(3,2*K,by=1),]
  FinalTable[seq(2*K-1,2*K,by=1),]<-Table[c(1,2),]
  
  return(FinalTable)
}

tables<-function(Mean,Vari,R=5000){
    
  Mean[,c("Q1","Q2","Q3","Q4")]<-4*Mean[,c("Q1","Q2","Q3","Q4")]
  Vari[,c("Q1","Q2","Q3","Q4")]<-16*Vari[,c("Q1","Q2","Q3","Q4")]
  
  StErr<-sqrt(Vari)
  
  Test<-abs(Mean/StErr)
  pvals<-2*apply(Test,c(1,2),pt,df=R-1,lower.tail=FALSE,log.p=FALSE)
  
  Stars<-matrix(rep("",nrow(Mean)*ncol(Mean)),nrow=nrow(Mean),ncol=ncol(Mean))
  Stars[0.05<pvals & pvals<=0.1]="*"
  Stars[0.01<pvals & pvals<=0.05]="**"
  Stars[pvals<=0.01]="***"
  rownames(Stars)<-rownames(Mean)
  colnames(Stars)<-colnames(Mean)
  
  Estim<-matrix(rep("",nrow(Mean)*ncol(Mean)),nrow=nrow(Mean),ncol=ncol(Mean))
  rownames(Estim)<-rownames(Mean)
  colnames(Estim)<-colnames(Mean)  
  Estim[!rownames(Mean)%in%"exper2",]<-apply(format(round(Mean[!rownames(Mean)%in%"exper2",],digits=4),scientific=FALSE),c(1,2),toString)
  Estim[rownames(Mean)%in%"exper2",]<-format(round(Mean[rownames(Mean)%in%"exper2",],digits=5),scientific=FALSE)
  
  ErrSt<-matrix(rep("",nrow(Mean)*ncol(Mean)),nrow=nrow(Mean),ncol=ncol(Mean))
  rownames(ErrSt)<-rownames(Mean)
  colnames(ErrSt)<-colnames(Mean)
  ErrSt[!rownames(Mean)%in%"exper2",]<-apply(format(round(StErr[!rownames(Mean)%in%"exper2",],digits=4),scientific=FALSE),c(1,2),toString)
  ErrSt[rownames(Mean)%in%"exper2",]<-format(round(StErr[rownames(Mean)%in%"exper2",],digits=5),scientific=FALSE)
  
  ErrSt<-matrix(paste(paste("(",ErrSt,sep=""),")",sep=""),nrow=nrow(Mean),ncol=ncol(Mean))
  rownames(ErrSt)<-rownames(Mean)
  colnames(ErrSt)<-colnames(Mean)
  
  Estim<-matrix(paste(Estim,Stars,sep=""),nrow=nrow(Mean),ncol=ncol(Mean))
  rownames(Estim)<-rownames(Mean)
  colnames(Estim)<-colnames(Mean)
  
  Table<-matrix(rep("",2*nrow(Mean)*ncol(Mean)),nrow=2*nrow(Mean),ncol=ncol(Mean))
  colnames(Table)<-colnames(Mean)
  Names<-matrix(rep("",2*nrow(Mean)),nrow=2*nrow(Mean),ncol=1)
  Names[seq(1,2*nrow(Mean),by=2)]<-rownames(Mean)
  Names[seq(2,2*nrow(Mean),by=2)]<-paste("SE_",rownames(Mean),sep="")
  rownames(Table)<-Names
  
  Table[seq(1,2*nrow(Mean),by=2),]<-Estim
  Table[seq(2,2*nrow(Mean),by=2),]<-ErrSt
  
  FinalTable<-matrix(rep("",2*nrow(Mean)*ncol(Mean)),nrow=2*nrow(Mean),ncol=ncol(Mean))
  colnames(FinalTable)<-colnames(Mean)
  FinalNames<-matrix(rep("",2*nrow(Mean)),nrow=2*nrow(Mean),ncol=1)
  FinalNames[seq(1,2*nrow(Mean)-2,by=1),]<-Names[seq(3,2*nrow(Mean),by=1),]
  FinalNames[seq(2*nrow(Mean)-1,2*nrow(Mean),by=1),]<-Names[c(1,2),]
  rownames(FinalTable)<-FinalNames
  FinalTable[seq(1,2*nrow(Mean)-2,by=1),]<-Table[seq(3,2*nrow(Mean),by=1),]
  FinalTable[seq(2*nrow(Mean)-1,2*nrow(Mean),by=1),]<-Table[c(1,2),]
  return(FinalTable)
}

tables_by<-function(directory="/home/hrtdmrt2/Desktop/3rdYearPaper",
                    vol="V2_Impact86_n",
                    by="Total"){
  column=by
  setwd(directory)
  mu<-NULL
  s2<-NULL
  colnam<-NULL
  for(n in seq(2,10,by=1)){
    m=paste(vol,n,"_mean.csv",sep="")
    v=paste(vol,n,"_variance.csv",sep="")
    
    Mean<-read.csv(m, header = TRUE, stringsAsFactor = FALSE)
    rownames(Mean)<-Mean[,"X"]
    Mean<-Mean[,!names(Mean)%in%"X"]
    Mean[,c("Q1","Q2","Q3","Q4")]<-4*Mean[,c("Q1","Q2","Q3","Q4")]
    
    Vari<-read.csv(v, header = TRUE, stringsAsFactor = FALSE)
    rownames(Vari)<-Vari[,"X"]
    Vari<-Vari[,!names(Vari)%in%"X"]
    Vari[,c("Q1","Q2","Q3","Q4")]<-16*Vari[,c("Q1","Q2","Q3","Q4")]
    
    mu<-cbind(mu,Mean[,column])
    s2<-cbind(s2,Vari[,column])
    
    colnam<-paste(colnam,'"',n,'"',", ",sep="")
  }
  rownames(mu)<-rownames(Mean)
  rownames(s2)<-rownames(Mean)
  colnam<-paste("c(",substr(colnam,1,nchar(colnam)-2),")")
  colnam<-eval(parse(text=colnam))
  colnames(mu)<-colnam
  colnames(s2)<-colnam
  rm(Mean)
  rm(Vari)
  Mean<-mu
  Vari<-s2
  
  StErr<-sqrt(Vari)
  
  Test<-abs(Mean/StErr)
  pvals<-2*apply(Test,c(1,2),pt,df=5000-1,lower.tail=FALSE,log.p=FALSE)
  
  Stars<-matrix(rep("",nrow(Mean)*ncol(Mean)),nrow=nrow(Mean),ncol=ncol(Mean))
  Stars[0.05<pvals & pvals<=0.1]="*"
  Stars[0.01<pvals & pvals<=0.05]="**"
  Stars[pvals<=0.01]="***"
  rownames(Stars)<-rownames(Mean)
  colnames(Stars)<-colnames(Mean)
  
  Estim<-matrix(rep("",nrow(Mean)*ncol(Mean)),nrow=nrow(Mean),ncol=ncol(Mean))
  rownames(Estim)<-rownames(Mean)
  colnames(Estim)<-colnames(Mean)  
  Estim[!rownames(Mean)%in%"exper2",]<-apply(format(round(Mean[!rownames(Mean)%in%"exper2",],digits=4),scientific=FALSE),c(1,2),toString)
  Estim[rownames(Mean)%in%"exper2",]<-do.call(cbind,lapply(format(round(Mean[rownames(Mean)%in%"exper2",],digits=5),scientific=FALSE),toString))
  
  ErrSt<-matrix(rep("",nrow(Mean)*ncol(Mean)),nrow=nrow(Mean),ncol=ncol(Mean))
  rownames(ErrSt)<-rownames(Mean)
  colnames(ErrSt)<-colnames(Mean)
  ErrSt[!rownames(Mean)%in%"exper2",]<-apply(format(round(StErr[!rownames(Mean)%in%"exper2",],digits=4),scientific=FALSE),c(1,2),toString)
  ErrSt[rownames(Mean)%in%"exper2",]<-do.call(cbind,lapply(format(round(StErr[rownames(Mean)%in%"exper2",],digits=5),scientific=FALSE),toString))
  
  ErrSt<-matrix(paste(paste("(",ErrSt,sep=""),")",sep=""),nrow=nrow(Mean),ncol=ncol(Mean))
  rownames(ErrSt)<-rownames(Mean)
  colnames(ErrSt)<-colnames(Mean)
  
  Estim<-matrix(paste(Estim,Stars,sep=""),nrow=nrow(Mean),ncol=ncol(Mean))
  rownames(Estim)<-rownames(Mean)
  colnames(Estim)<-colnames(Mean)
  
  Table<-matrix(rep("",2*nrow(Mean)*ncol(Mean)),nrow=2*nrow(Mean),ncol=ncol(Mean))
  colnames(Table)<-colnames(Mean)
  Names<-matrix(rep("",2*nrow(Mean)),nrow=2*nrow(Mean),ncol=1)
  Names[seq(1,2*nrow(Mean),by=2)]<-rownames(Mean)
  Names[seq(2,2*nrow(Mean),by=2)]<-paste("SE_",rownames(Mean),sep="")
  rownames(Table)<-Names
  
  Table[seq(1,2*nrow(Mean),by=2),]<-Estim
  Table[seq(2,2*nrow(Mean),by=2),]<-ErrSt
  
  FinalTable<-matrix(rep("",2*nrow(Mean)*ncol(Mean)),nrow=2*nrow(Mean),ncol=ncol(Mean))
  colnames(FinalTable)<-colnames(Mean)
  FinalNames<-matrix(rep("",2*nrow(Mean)),nrow=2*nrow(Mean),ncol=1)
  FinalNames[seq(1,2*nrow(Mean)-2,by=1),]<-Names[seq(3,2*nrow(Mean),by=1),]
  FinalNames[seq(2*nrow(Mean)-1,2*nrow(Mean),by=1),]<-Names[c(1,2),]
  rownames(FinalTable)<-FinalNames
  FinalTable[seq(1,2*nrow(Mean)-2,by=1),]<-Table[seq(3,2*nrow(Mean),by=1),]
  FinalTable[seq(2*nrow(Mean)-1,2*nrow(Mean),by=1),]<-Table[c(1,2),]
  return(FinalTable)
}

save.xlsx <- function (file, ...){
  require(xlsx, quietly = TRUE)
  objects <- list(...)
  fargs <- as.list(match.call(expand.dots = TRUE))
  objnames <- as.character(fargs)[-c(1, 2)]
  nobjects <- length(objects)
  for (i in 1:nobjects) {
    if (i == 1)
      write.xlsx(objects[[i]], file, sheetName = objnames[i])
    else write.xlsx(objects[[i]], file, sheetName = objnames[i],
                    append = TRUE)
  }
  print(paste("Workbook", file, "has", nobjects, "worksheets."))
}





